﻿using System;
using System.Windows.Forms;

using Daq331m;

namespace PWM_Demo
{
    public partial class Form1 : Form
    {
        private readonly Daq331M _daq331;
        public Form1()
        {
            InitializeComponent();
            
            _daq331 = new Daq331M();
        }

        private void ConnectPushButton_Click(object sender, EventArgs e)
        {
            _daq331.DeleteDevice();
            _daq331.CreateDevice();
            
            _daq331.UseTcpClientBackend("192.168.1.20", 8000);

            // 初始化设备
            Console.WriteLine(!_daq331.InitializeDevice() ? "DAQ331M设备初始化失败" : "DAQ331M设备初始化成功");
        }

        private void StartPWM1PushButton_Click(object sender, EventArgs e)
        {
            var frequent = Convert.ToUInt32(PWM1FrequentLineEdit.Text);
            var duty = Convert.ToUInt32(PWM1DutyCycleLineEdit.Text);
            
            var configResult = _daq331.ConfigPwm((uint)DaqPwmChannel.Pwm1, frequent, duty);
            Console.WriteLine(!configResult ? @"DAQ331M配置PWM1失败" : @"DAQ331M配置PWM1成功");
            var startResult = _daq331.StartPwm((uint)DaqPwmChannel.Pwm1);
            Console.WriteLine(!startResult ? @"DAQ331M开启PWM1失败" : @"DAQ331M开启PWM1成功");
        }

        private void StopPWM1PushButton_Click(object sender, EventArgs e)
        {
            var stopResult = _daq331.StopPwm((uint)DaqPwmChannel.Pwm1);
            Console.WriteLine(!stopResult ? @"DAQ331M停止PWM1失败" : @"DAQ331M停止PWM1成功");
        }

        private void StartPWM2PushButton_Click(object sender, EventArgs e)
        {
            var frequent = Convert.ToUInt32(PWM2FrequentLineEdit.Text);
            var duty = Convert.ToUInt32(PWM2DutyCycleLineEdit.Text);
            
            var configResult = _daq331.ConfigPwm((uint)DaqPwmChannel.Pwm2, frequent, duty);
            Console.WriteLine(!configResult ? @"DAQ331M配置PWM1失败" : @"DAQ331M配置PWM1成功");
            var startResult = _daq331.StartPwm((uint)DaqPwmChannel.Pwm2);
            Console.WriteLine(!startResult ? @"DAQ331M开启PWM2失败" : @"DAQ331M开启PWM2成功");
        }

        private void StopPWM2PushButton_Click(object sender, EventArgs e)
        {
            var stopResult = _daq331.StopPwm((uint)DaqPwmChannel.Pwm2);
            Console.WriteLine(!stopResult ? @"DAQ331M停止PWM2失败" : @"DAQ331M停止PWM2成功");
        }
    }
}
